package com.apollow.oschina.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.apollow.oschina.R;
import com.apollow.oschina.adapter.NewsRvAdapter;
import com.apollow.oschina.base.BaseFragment;
import com.apollow.oschina.base.Global;
import com.apollow.oschina.bean.GeneralNewsBean;
import com.apollow.oschina.protocol.NewsProtocol;
import com.apollow.oschina.view.ContentPager;
import com.apollow.oschina.view.RecycleViewDivider;

import java.util.List;

public class DefaultFragment extends BaseFragment {
    public List<GeneralNewsBean.NewsBean> list;
    public ContentPager mContentPager;
    private RecyclerView mRecycleView;
    private NewsRvAdapter mHomeAdapter;
    private NewsProtocol mProtocol;
    private SwipeRefreshLayout mRefreshLayout;
    private int pageIndex;
    private LinearLayoutManager mLayoutManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = Global.layoutInflate(R.layout.fragment_base);
        mRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_reflash);
        mRecycleView = (RecyclerView) view.findViewById(R.id.recycleView);
        mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mRecycleView.setLayoutManager(mLayoutManager);
        mProtocol = new NewsProtocol();
        mHomeAdapter = new NewsRvAdapter(getActivity(),mProtocol);
        mRecycleView.addItemDecoration(new RecycleViewDivider(getContext(),LinearLayoutManager.HORIZONTAL));
        mRecycleView.setAdapter(mHomeAdapter);
        return view;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initEvent();
    }
    private void initEvent() {
        mRefreshLayout.setColorSchemeColors(Color.GREEN,Color.BLACK,Color.CYAN,Color.BLUE,Color.YELLOW);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Global.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        list.clear();
                        mProtocol.setPageIndex(String.valueOf(++pageIndex));
                        System.out.println(String.valueOf(pageIndex));
                        initData();
                        mRefreshLayout.setRefreshing(false);
                        Global.showToast("下拉刷新完成");
                    }
                }, 1500);
            }
        });
        mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            int page = 0;
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
                //加载更多
                if ((list.size()-1) == lastVisibleItem){
                    /*if (page < 4){
                        page++;
                    }*/
//                    initData(page+"");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            list.addAll(mProtocol.getData(page+"").getNewslist());
                            mActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mHomeAdapter.setData(list);
                                }
                            });
                        }
                    }).start();
                }
            }
        });
    }
    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mHomeAdapter.mlist.clear();
                list = mProtocol.getData("0").getNewslist();
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mHomeAdapter.setData(list);
                    }
                });
            }
        }).start();
    }
}
